#include<iostream>
#include<signal.h>
#include"../comm/httplib.h"
#include"oj_control.hpp"
#include"../comm/log.hpp"
using namespace httplib;
using namespace ns_control;
using namespace ns_log;

static control* _con_ptr = nullptr;

void OnlineMachine(int)
{
    _con_ptr->OnlineMachines();
}

int main()
{
    signal(SIGQUIT,OnlineMachine);

    //用户请求的路由功能
    Server svr;
    control _control;
    _con_ptr = &_control;
    //获取所有题目
    svr.Get("/all_question",[&_control](const Request& req,Response& resp){
        std::string html;
        _control.AllQuestionHtml(&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //获取单个题目
    svr.Get(R"(/question/(\d+))",[&_control](const Request& req,Response& resp){
        std::string number = req.matches[1];
        std::string html;
        _control.OneQuestionHtml(number,&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //判断某个题目
    svr.Post(R"(/judge/(\d+))",[&_control](const Request& req,Response& resp){
        std::string number = req.matches[1];
         std::string html;
         _control.Judge(number,req.body,&html);
         resp.set_content(html,"application/json;charset=utf-8");
        // resp.set_content("正在判题编号: "+number,"text/plain;charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",8080);
    return 0;
}
